package com.ailiwu.www.ailiwu.Fragment;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.widget.ListView;

import com.ailiwu.www.ailiwu.R;
import com.ailiwu.www.ailiwu.adapter.MyListViewAdapter;
import com.ailiwu.www.ailiwu.base.BaseFragment;
import com.ailiwu.www.ailiwu.bean.HomeHotF4Icon;
import com.ailiwu.www.ailiwu.bean.ItemBean;
import com.ailiwu.www.ailiwu.url.Urls;
import com.ailiwu.www.ailiwu.utils.Logger;
import com.ailiwu.www.ailiwu.views.ImageTextButton;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.ArrayList;
import java.util.List;

/**
 * Author:    XiYoung
 * Version    V1.0
 * Date:      2015/12/4 000421:08
 * Description:
 * Modification  History:
 * Date         	Author        		Version        	Description
 * -----------------------------------------------------------------------------------
 * 2015/12/4 0004       XiYoung              1.0                   1.0
 * Why & What is modified:
 */
public class ListViewFragment extends BaseFragment {
    private ListView listView;
    private ImageTextButton imageTextButton1;
    private ImageTextButton imageTextButton2;
    private ImageTextButton imageTextButton3;
    private ImageTextButton imageTextButton4;
    private ArrayList<ItemBean.DataEntity.ItemsEntity> itemsEntities = new ArrayList<>();
    private MyListViewAdapter adapter;

    @Override
    protected View inflateView() {
        return View.inflate(getActivity(), R.layout.fragement_home_first_listview, null);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    protected void initView(View view) {
        View headVivew = View.inflate(getActivity(), R.layout.layout_adbanner, null);
        imageTextButton1 = (ImageTextButton) headVivew.findViewById(R.id.image_text_button1);
        imageTextButton2 = (ImageTextButton) headVivew.findViewById(R.id.image_text_button2);
        imageTextButton3 = (ImageTextButton) headVivew.findViewById(R.id.image_text_button3);
        imageTextButton4 = (ImageTextButton) headVivew.findViewById(R.id.image_text_button4);



        listView = (ListView) view.findViewById(R.id.listview);
        listView.addHeaderView(headVivew);
        adapter = new MyListViewAdapter(mAcAvtivity, itemsEntities);


        listView.setAdapter(adapter);

    }

    @Override
    protected void initData() {
        updataF4Icon();
        updataRecycler();
    }

    private void updataRecycler() {
        httpUtils.send(HttpRequest.HttpMethod.GET, Urls.URL_HOME_RECYCLERVIEW, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                //TODO 存入数据库
                if (result != null) {
                    ItemBean itemBean = gson.fromJson(result, ItemBean.class);
                    List<ItemBean.DataEntity.ItemsEntity> items = itemBean.getData().getItems();
                    itemsEntities.addAll(items);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                // TODO: 2015/12/3 0003 网络连接失败
                Snackbar.make(imageTextButton1, "哎呀！网络连接失败了！！", Snackbar.LENGTH_LONG).show();
            }
        });

    }

    private void updataF4Icon() {
        httpUtils.send(HttpRequest.HttpMethod.GET, Urls.URL_HOME_F4_ICON, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                Logger.i(result);
                //TODO 存入数据库
                parseF4FromJson(result);
            }


            @Override
            public void onFailure(HttpException e, String s) {
                // TODO: 2015/12/3 0003 网络连接失败
                Snackbar.make(imageTextButton1, "哎呀！网络连接失败了！！", Snackbar.LENGTH_LONG).show();
            }
        });
    }

    /**
     * 设置F4的图片属性
     */
    protected void setF4Icon(ImageTextButton imageTextButton, String color, String imageUrl, String title) {
        imageTextButton.setTextColor(color);
        imageTextButton.setText(title);
        imageTextButton.setImageBitmapFromUrl(imageUrl);
    }

    /**
     * 解析Json并且设置F4图片、文字、颜色
     */
    protected void parseF4FromJson(String result) {
        if (result != null) {
            HomeHotF4Icon homeHotF4Icon = gson.fromJson(result, HomeHotF4Icon.class);
            List<HomeHotF4Icon.DataEntity.PromotionsEntity> promotions = homeHotF4Icon.getData().getPromotions();
            for (int i = 0; i < promotions.size(); i++) {
                String color = promotions.get(i).getColor();
                String imageUrl = promotions.get(i).getIcon_url();
                String title = promotions.get(i).getTitle();
                switch (i) {
                    case 0:
                        setF4Icon(imageTextButton1, color, imageUrl, title);
                        break;
                    case 1:
                        setF4Icon(imageTextButton2, color, imageUrl, title);
                        break;
                    case 2:
                        setF4Icon(imageTextButton3, color, imageUrl, title);
                        break;
                    case 3:
                        setF4Icon(imageTextButton4, color, imageUrl, title);
                        break;
                    default:
                        break;
                }

            }

        }
    }

}


